'''
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License") you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
'''
from base.datatypes.xsd.AbstractDateTime import AbstractDateTime
from base.datatypes.xsd.XSDDateTime import XSDDateTime
from base.datatypes.xsd.impl.XSDAbstractDateTimeType import XSDAbstractDateTimeType

'''
 * Type processor for year, most of the machinery is in the
 * base XSDAbstractDateTimeType class.
'''
from base.datatypes.BaseDatatype import BaseDatatype

class XSDYearType(XSDAbstractDateTimeType):

    '''
     * Constructor
    '''
    # param
    # return
    def __init__(self, typename):
        super().__init__(typename)
    # XSDYearType(String typename):
    #     super(typename)
    

    '''
     * Parse a validated date. This is invoked from
     * XSDDatatype.convertValidatedDataValue rather then from a local
     * parse method to make the implementation of XSDGenericType easier.
    '''
    #@Override
    # param String
    # return Object
    def parseValidated(self, str):
        # int
        len = str.length()
        #int[] date = int[TOTAL_SIZE]
        #int[] timeZone = int[2]
        date = []
        timeZone = []

        # check for preceding '-' sign
        # int
        start = 0
        if (str.charAt(0) == '-'):
            start = 1
        
        # int
        sign = self.findUTCSign(str, start, len)
        global CY
        global M
        global D
        if (sign == -1):
            date[CY] = self.parseIntYear(str, len)
        else:
            date[CY] = self.parseIntYear(str, sign)
            self.getTimeZone(str, date, sign, len, timeZone)
        

        #initialize values
        date[M] = self.MONTH
        date[D] = 1

        global utc
        if (date[utc] != 0 and date[utc] != 'Z'):
            AbstractDateTime.normalize(date, timeZone)
        

        return XSDDateTime(date, self.YEAR_MASK)
    


